package com.angis.fx.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.angis.fx.data.DraftInfo;
import com.angis.fx.util.ContentValuesUtil;

public class CheckDraftDBHelper extends ObjectDBHelper {

	public static final String PK_ID = "id";
	public static final String COLUMN_CSID = "csid";
	public static final String COLUMN_CONTITLE = "contitle";
	public static final String COLUMN_ADDRESS = "address";
	public static final String COLUMN_MANAGER = "manager";
	public static final String COLUMN_AREATYPE = "areatype";
	public static final String COLUMN_PERMITCODE = "permitcode";
	public static final String COLUMN_PERMITWORD = "permitword";
	public static final String COLUMN_AREA = "area";
	public static final String COLUMN_AREACODE = "areacode";
	public static final String COLUMN_HYCODE = "hycode";
	public static final String COLUMN_CODE = "code";
	public static final String COLUMN_MANAGEPOS = "managepos";
	public static final String COLUMN_TEL = "tel";
	public static final String COLUMN_UNITTYPE = "unittype";
	public static final String COLUMN_JCJG = "jcjg";
	public static final String COLUMN_JCSJ = "jcsj";
	public static final String COLUMN_JCQK = "jcqk";
	public static final String COLUMN_JCJGDETAIL = "jcjgdetail";
	public static final String COLUMN_TOGETHER = "together";
	public static final String COLUMN_OTHERUSER = "otheruser";
	public static final String COLUMN_WHCSDUTYID = "whcsdutyid";
	public static final String COLUMN_TJGUID = "tjguid";
	public static final String COLUMN_CSLB = "cslb";
	public static final String COLUMN_USER_NAME = "user_name";
	public static final String COLUMN_ARRIVALTIME = "arrivaltime";
	public static final String COLUMN_LEAVETIME = "leavetime";
	public static final String COLUMN_DUTYNO = "dutyno";
	public static final String COLUMN_DJGUID = "djguid";
	public static final String COLUMN_CHECKXS = "checkxs";
	public static final String COLUMN_CDRC = "cdrc";
	public static final String COLUMN_CHECKAREA = "checkarea";
	public static final String COLUMN_DATASTATUS = "datastatus";
	public static final String COLUMN_TYCODE = "tycode";
	public static final String COLUMN_X = "x";
	public static final String COLUMN_Y = "y";
	public static final String COLUMN_XLONGPS = "xlongps";
	public static final String COLUMN_YLATGPS = "ylatgps";
	public static final String COLUMN_CHECKTIME = "checktime";

	public static final String TABLE_NAME = "checkdraft";

	public CheckDraftDBHelper(Context context) {
		super(context);
	}

	public long insert(ContentValues lValues) {
		return this.insert(TABLE_NAME, lValues);
	}
	public void updateDraft(DraftInfo info){
		if (null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		ContentValues lValues = ContentValuesUtil.convertCheckDraft(info);
		readDB.update(TABLE_NAME, lValues, PK_ID+"=?", new String[]{info.getId()});
		readDB.close();
	}
	public List<DraftInfo> findDraftByType() {
		List<DraftInfo> lResultList = new ArrayList<DraftInfo>();
		if (null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		Cursor lCursor = readDB.query(TABLE_NAME, new String[]{COLUMN_AREA, COLUMN_ARRIVALTIME, COLUMN_CHECKXS}, COLUMN_DATASTATUS + "=?", new String[]{"1"}, null, null, null);
		if(lCursor.moveToFirst()) {
			do {
				DraftInfo lInfo=new DraftInfo();
				lInfo.setArea(lCursor.getString(0));
				lInfo.setArrivaltime(lCursor.getString(1));
				lInfo.setCheckxs(lCursor.getString(2));
				lResultList.add(lInfo);
			} while (lCursor.moveToNext());
		}
		lCursor.close();
		readDB.close();
		return lResultList;
	}

	public List<DraftInfo> findDraftByPath(int index,String datatype) {
		List<DraftInfo> list = new ArrayList<DraftInfo>();
		if (null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		String sql = "select * from " + TABLE_NAME + " where "+COLUMN_DATASTATUS+" =? Limit "
				+ String.valueOf(10) + " Offset "
				+ String.valueOf((index - 1) * 10);
		Cursor rec = readDB.rawQuery(sql, new String[]{datatype});
		int recCount = rec.getCount();// 获得数据条数
		for (int i = 0; i < recCount; i++) {
			rec.moveToPosition(i);
			list.add(toDraftInfo(rec));
		}
		rec.close();
		return list;
	}

	public int totalCount() {
		if (null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		String sql = "select count(*) from " + TABLE_NAME;
		Cursor cursor = readDB.rawQuery(sql, null);
		cursor.moveToPosition(0);
		int i = cursor.getInt(0);
		cursor.close();
		return i;
	}

	public DraftInfo findById(String id) {
		DraftInfo draftInfo;
		if (null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		String sql = "select * from " + TABLE_NAME + " where " + PK_ID + " =?";
		Cursor cursor = readDB.rawQuery(sql, new String[] { id });
		cursor.moveToPosition(0);
		draftInfo=toDraftInfo(cursor);
		cursor.close();
		return draftInfo;
	}

	private DraftInfo toDraftInfo(Cursor rec) {
		DraftInfo info = new DraftInfo();
		info.setId(rec.getString(rec.getColumnIndex(PK_ID)));
		info.setCsid(rec.getString(rec.getColumnIndex(COLUMN_CSID)));
		info.setContitle(rec.getString(rec.getColumnIndex(COLUMN_CONTITLE)));
		info.setAddress(rec.getString(rec.getColumnIndex(COLUMN_ADDRESS)));
		info.setManager(rec.getString(rec.getColumnIndex(COLUMN_MANAGER)));
		info.setAreatype(rec.getString(rec.getColumnIndex(COLUMN_AREATYPE)));
		info
				.setPermitcode(rec.getString(rec
						.getColumnIndex(COLUMN_PERMITCODE)));
		info
				.setPermitword(rec.getString(rec
						.getColumnIndex(COLUMN_PERMITWORD)));
		info.setArea(rec.getString(rec.getColumnIndex(COLUMN_AREA)));
		info.setAreacode(rec.getString(rec.getColumnIndex(COLUMN_AREACODE)));
		info.setHycode(rec.getString(rec.getColumnIndex(COLUMN_HYCODE)));
		info.setCode(rec.getString(rec.getColumnIndex(COLUMN_CODE)));
		info.setManagepos(rec.getString(rec.getColumnIndex(COLUMN_MANAGEPOS)));
		info.setTel(rec.getString(rec.getColumnIndex(COLUMN_TEL)));
		info.setUnittype(rec.getString(rec.getColumnIndex(COLUMN_UNITTYPE)));
		info.setJcjg(rec.getString(rec.getColumnIndex(COLUMN_JCJG)));
		info.setJcsj(rec.getString(rec.getColumnIndex(COLUMN_JCSJ)));
		info.setJcqk(rec.getString(rec.getColumnIndex(COLUMN_JCQK)));
		info
				.setJcjgdetail(rec.getString(rec
						.getColumnIndex(COLUMN_JCJGDETAIL)));
		info.setTogether(rec.getString(rec.getColumnIndex(COLUMN_TOGETHER)));
		info.setOtheruser(rec.getString(rec.getColumnIndex(COLUMN_OTHERUSER)));
		info
				.setWhcsdutyid(rec.getString(rec
						.getColumnIndex(COLUMN_WHCSDUTYID)));
		info.setTjguid(rec.getString(rec.getColumnIndex(COLUMN_TJGUID)));
		info.setCslb(rec.getString(rec.getColumnIndex(COLUMN_CSLB)));
		info.setUser_name(rec.getString(rec.getColumnIndex(COLUMN_USER_NAME)));
		info.setArrivaltime(rec.getString(rec
				.getColumnIndex(COLUMN_ARRIVALTIME)));
		info.setLeavetime(rec.getString(rec.getColumnIndex(COLUMN_LEAVETIME)));
		info.setDutyno(rec.getString(rec.getColumnIndex(COLUMN_DUTYNO)));
		info.setDjguid(rec.getString(rec.getColumnIndex(COLUMN_DJGUID)));
		info.setCheckxs(rec.getString(rec.getColumnIndex(COLUMN_CHECKXS)));
		info
				.setDatastatus(rec.getString(rec
						.getColumnIndex(COLUMN_DATASTATUS)));
		info.setTycode(rec.getString(rec.getColumnIndex(COLUMN_TYCODE)));
		info.setX(rec.getString(rec.getColumnIndex(COLUMN_X)));
		info.setY(rec.getString(rec.getColumnIndex(COLUMN_Y)));
		info.setXlongps(rec.getString(rec.getColumnIndex(COLUMN_XLONGPS)));
		info.setYlatgps(rec.getString(rec.getColumnIndex(COLUMN_YLATGPS)));
		info.setChecktime(rec.getString(rec.getColumnIndex(COLUMN_CHECKTIME)));
		return info;
	}
}
